package server.command.list;

import java.lang.reflect.InvocationTargetException;
import java.util.Collection;
import java.util.LinkedList;

import communication.exception.ResultException;
import communication.exception.VerifyParameterException;

import server.command.AnalysisCommand;
import server.command.Command;
import server.core.base.Ticket;
import server.core.dao.DaoFactory;
import server.core.manager.Customer;
import server.core.manager.Manager;

public class CancelReserve implements Command {

	private AnalysisCommand parsing;

	@Override
	public Collection<Object> execute() throws ResultException {
		Collection<Object> result = new LinkedList<Object>();
		Long ticket = null;
		try {
			ticket = ((Customer) getManager().getConstructor(parsing.getTypeOfDao().getClass()).newInstance(parsing.getTypeOfDao()))
					.cancelTicket((Ticket) parsing.getArgsMap().get("ID"));
		} catch (InstantiationException | IllegalAccessException
				| IllegalArgumentException | InvocationTargetException
				| NoSuchMethodException | SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(ticket==null)
			throw new ResultException("Ticket with this ID is already available");
		result.add(Long.valueOf(ticket));
		result.add("-id cancel reserve!");
		return result;
	}

	@Override
	public void verifyParam(AnalysisCommand parsing)
			throws VerifyParameterException {
		this.parsing=parsing;
		for(String key:AnalysisCommand.paramId){
			if(parsing.getParamMap().containsKey(key)){
				Ticket ticket=(Ticket) DaoFactory.getDao(parsing.getTypeOfDao()).getObject(Ticket.class, Long.parseLong(parsing.getParamMap().get(key)));
					if(ticket==null)
						throw new VerifyParameterException("There isn`t ticket with this id");
					else
						parsing.getArgsMap().put(key, ticket);
			}
			else
				throw new VerifyParameterException(
						"Parameter Error. Type \"help <command>\" to print list of available parameters");
		}
	}

	@Override
	public String help() {
		return " id=<Ticket>";
	}

	@Override
	public Class<? extends Manager> getManager() {
		return Customer.class;
	}

	@Override
	public void exit() {
		// TODO Auto-generated method stub
		
	}

}
